package org.tongdao.yulong.withanno;

import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @Description: TODO
 * @author: 殷玉龙
 * @date: 2021年01月11日 下午3:32
 */
public class ImmediatlyLookupApplication {
  public static void main(String[] args) {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("basic_dl/quickstart-lazylookup.xml");
    Cat cat = ctx.getBean(Cat.class);
    System.out.println(cat);

//    Dog dog;
//    try {
//      dog = ctx.getBean(Dog.class);
//    } catch (NoSuchBeanDefinitionException e) {
//      // 找不到Dog时手动创建
//      dog = new Dog();
//    }
//    System.out.println(dog);

    //改良
    Dog dog = ctx.containsBean("dog") ? (Dog) ctx.getBean("dog") : new Dog();
    System.out.println(dog);
  }
}
